﻿using AnlianCommon.Common.Domains.BankCustomers;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;

namespace SheepCommon.API.SheepSelles
{
    public class SheepSellCreateHandler : IRequestHandler<SheepSellCreateCommand, Result>
    {
        private readonly IBaseRepository<SheepSell> _sheepSellRepository;
        private readonly IMapper _mapper;

        public SheepSellCreateHandler(IBaseRepository<SheepSell> sheepSellRepository, IMapper mapper)
        {
            _sheepSellRepository = sheepSellRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 销售新增业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(SheepSellCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code=(int)CommonErrorCode.Added};
            try
            {
                SheepSell sheepSell = _mapper.Map<SheepSell>(request);
                var res = await _sheepSellRepository.Add(sheepSell);
                if (res > 0)
                {
                    result.Code=(int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
                else
                {
                    result.Msg = "添加失败";
                }
                
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
